#include "stack.h"
#include <cstring>

using namespace stackex;



Stack::Stack()
{
    this->m_pData = new int[INIT_STACK_SIZE];
    this->m_size = 0;
    this->m_cap = INIT_STACK_SIZE;
}

Stack::~Stack()
{
    if (this->m_pData)
    {
        delete this->m_pData;
    }
}

void Stack::StackPush(int data)
{
    if (this->m_size == this->m_cap)
    {
        int *old = this->m_pData;
        this->m_pData = new int[this->m_cap*2];
        memcpy(this->m_pData, old, this->m_size*sizeof(int));
        delete old;
    }

    this->m_pData[this->m_size++] = data;
}

int Stack::StackPop(int &data)
{
    if (this->m_size == 0)
    {
        return -1;
    }

    data =  this->m_pData[this->m_size-1];
    --this->m_size;

    return 0;
}

int Stack::StackTop(int &data)
{
    if (this->m_size  == 0)
    {
        return -1;
    }

    data = this->m_pData[this->m_size-1];
    return 0;
}

int Stack::StackSize()
{
    return this->m_size;
}
